<template>
  <!-- <div class="cardHeader flexbox justifyBetween">
    <span>充电设备趋势</span>
    <el-radio-group v-model="time" size="small" @change="trendQueryChange">
      <el-radio-button :label="1">今日</el-radio-button>
      <el-radio-button :label="2">昨日</el-radio-button>
      <el-radio-button :label="3">本月</el-radio-button>
      <el-radio-button :label="4">本年</el-radio-button>
    </el-radio-group>
  </div>
  <el-tabs v-model="activeName" @tab-click="trendQueryChange">
    <el-tab-pane label="总收益趋势" name="1"></el-tab-pane>
    <el-tab-pane label="总放电趋势" name="2"></el-tab-pane>
  </el-tabs> -->
  <!-- <div class="chartTitleBox flexbox justifyStart">
    <img class="chartTitleIcon" src="@/assets/imgs/iconIncome.png" v-if="activeName === '1'" />
    <img class="chartTitleIcon" src="@/assets/imgs/iconDischarge.png" v-else />
    <p class="chartTitle">
      总收益
      <span class="chartTitleNum">{{ chartData.total }}</span>
      元
    </p>
  </div> -->
  <div ref="chartContainer" v-chart-resize class="chartContainer"></div>
</template>

<script setup>
import { ref, onMounted, onBeforeUnmount, watch } from 'vue'
import * as echarts from 'echarts'
import 'echarts/theme/macarons' // echarts theme

const props = defineProps({
  chartData: {
    type: Object,
    default: () => ({})
  }
})

const emit = defineEmits(['trendQueryChange'])

// 响应式数据
const time = ref(1)
const activeName = ref('1')
const chart = ref(null)
const chartContainer = ref(null)

// 初始化图表
const initChart = () => {
  chart.value = echarts.init(chartContainer.value)
  setOptions(props.chartData)
}

// 设置图表选项
const setOptions = (data) => {
  if (!chart.value) return

  chart.value.setOption({
    grid: {
      top: '14%',
      bottom: 5,
      left: 10,
      right: 10,
      containLabel: true
    },
    dataset: {
      source: data.dataList || []
    },
    tooltip: {
      trigger: 'item',
      formatter: (params) => {
        return `收益<br />${params.name}：${params.value.y}`
      }
    },
    xAxis: {
      type: 'category',
      axisLabel: {
        fontSize: 22
      }
    },
    yAxis: {
      name: '收益：元',
      type: 'value',
      nameGap: 30,
      nameTextStyle: {
        fontSize: 24,
        color: '#fff'
      },
      axisLabel: {
        fontSize: 22
      },
      splitLine: {
        show: false
      }
    },
    series: [
      {
        type: 'bar',
        barWidth: '40%',
        label: {
          fontSize: 22,
          color: '#fff'
        },
        itemStyle: {
          color: '#1890FF',
          borderRadius: [4, 4, 0, 0]
        }
      }
    ]
  })
}

// 趋势查询变化
const trendQueryChange = () => {
  emit('trendQueryChange', {
    time: time.value,
    type: activeName.value
  })
}

// 监听数据变化
watch(
  () => props.chartData,
  (val) => {
    setOptions(val)
  },
  { deep: true }
)

// 生命周期钩子
onMounted(() => {
  initChart()
})

// onBeforeUnmount(() => {
//   if (chart.value) {
//     chart.value.dispose()
//     chart.value = null
//   }
// })
</script>

<style scoped>
.chartContainer {
  width: 100%;
  height: 100%;
}

.cardHeader {
  height: 28px;
}

.chartTitleIcon {
  width: 25px;
  height: 25px;
  margin-right: 8px;
}

.chartTitle {
  height: 25px;
  margin: 0;
  font-size: 13px;
  line-height: 25px;
  color: #9e9e9e;
}

.chartTitleNum {
  font-size: 16px;
  font-weight: 500;
  color: #fa4700;
}
</style>
